<template>
  <div id='Login'>
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/头像.png" alt="">
      </div>
      <!-- 表单登录 -->
      <el-form ref="loginFromRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input show-password prefix-icon="iconfont icon-mima" v-model="loginForm.password" placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <!-- 提交,重置 -->
        <el-form-item class="btns_sub">
          <el-button type="primary" @click="submitLoginForm('loginFromRef')">提交</el-button>
          <el-button type="info" @click="resetLoginForm('loginFromRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pp">
      <a href="https://beian.miit.gov.cn/" target="_blank">备案号：冀ICP备2022020215号</a>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm(loginFromRef) {
      this.$refs[loginFromRef].resetFields()
    },
    submitLoginForm(loginFromRef) {
      this.$refs[loginFromRef].validate(async (valid) => {
        if (!valid) return this.$message.error('表单数据提交失败,请重新提交')
        // const results = this.$http.post('login', this.loginForm)
        // console.log(results)
        const results = await this.$http.post('login', this.loginForm)
        if (results.meta.status !== 200) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        window.sessionStorage.setItem('token', results.data.token)
        await this.$router.push('/home')
      })
    }
  }
}

</script>

<style lang="less" scoped>
#Login {
  height: 100%;
  background-color: #525288;
  position: relative;
}

.pp {
  width: auto;
  height: auto;
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);

  a {
    color: black;
    text-align: center;
  }
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #482936;

  .avatar_box {
    box-sizing: border-box;
    width: 142px;
    height: 142px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    img {
      width: 130px;
      height: 130px;
      padding: 5px;
      border: 1px solid gray;
      border-radius: 50%;
      background-color: #b598a1;
    }
  }
}

.btns_sub {
  display: flex;
  justify-content: space-evenly;
}

.el-button {
  margin: 0 50px;
}

.login_form {
  box-sizing: border-box;
  padding: 0 50px;
  position: absolute;
  bottom: 10px;
  width: 100%;
}

/deep/ .el-input--prefix .el-input__inner {
  padding-left: 24px;
}
</style>
